package com.atguigu.gulimall.vo;

import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import lombok.Data;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author ming
 * @create 2022/5/5
 * @description: 整个购物车
 */
@Data
public class Cart {
    private List<CartItem> items;
    private Integer count;//商品数量
    private Integer countType;//商品类型数量
    private BigDecimal totalAmount;//商品总价
    private BigDecimal reduce=new BigDecimal("0");//减免价格

    public Integer getCount() {
        int count = 0;
        if(items != null && items.size()>0){
            for (CartItem item : items) {
               count+=item.getCount();
            }
        }
        return count;
    }

    public Integer getCountType() {
        int count = 0;
        if(items != null && items.size()>0){
            for (CartItem item : items) {
                count+=1;
            }
        }
        return count;
    }

    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal("0");
        // 计算购物项总价
        if (!CollectionUtils.isEmpty(items)) {
            for (CartItem cartItem : items) {
                if (cartItem.getCheck()) {
                    amount = amount.add(cartItem.getTotalPrice());
                }
            }
        }
        // 计算优惠后的价格
        return amount.subtract(getReduce());
    }
}
